package me.nereo.multi_image_selector.utils;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 文件操作类
 * Created by Nereo on 2015/4/8.
 */
public class FileUtils {
    /**
     *
     * @param context Context
     * @return crete File
     */
    public static File getDicmOrCach(Context context) {

        String state = Environment.getExternalStorageState();
        File parent = null;
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            // 已挂载
//            mParent = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            parent = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/Camera");
            if (!parent.exists()) {
                parent = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
            }
        } else {
            parent = context.getCacheDir();
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
        String fileName = "ali_music" + timeStamp + ".jpg";
        File tmpFile = new File(parent, fileName);
        return tmpFile;
    }

    /**
     * Check the SD card
     *
     * @return true have scard
     */
    public static boolean checkSDCardAvailable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * delete
     * @param folder File
     * @param fileName name
     */
    public static void deletePhotoAtPathAndName(File folder, String fileName) {
        if (checkSDCardAvailable()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].getName().equals(fileName)) {
                        files[i].delete();
                    }
                }
            }
        }
    }
}
